using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class SkillTrigger_ExecuteSkill : SkillTrigger
    {
        protected override ESkillTriggerType triggerType => ESkillTriggerType.ExecuteSkill;

        private ESkillType skillType = ESkillType.None;
        private ETargetRelation relation = ETargetRelation.None;
        private bool trigger;
        private bool inputExecuter;

        protected override void OnInitialize(Skill rSkill, TriggerConfig rConfig)
        {
            base.OnInitialize(rSkill, rConfig);
            if (rConfig.customConfig is SkillTrigger_ExecuteSkill_ConfigData rConfigData)
            {
                this.skillType = (ESkillType)rConfigData.skillType;
                this.relation = (ETargetRelation)rConfigData.relation;
                this.trigger = rConfigData.trigger;
                this.inputExecuter = rConfigData.inputExecuter;
            }
        }


        protected override void OnEnable()
        {
            base.OnEnable();
            this.AddListener<SkillPart, SelectParam, object, bool>(EBattleTriggerType.Battle_ExecuteSkill, this.OnEvent);
        }

        private void OnEvent(SkillPart skillPart, SelectParam input, object param, bool bTrigger)
        {
            var rSkill = skillPart.skill;
            if ((this.trigger || !bTrigger) &&
                (this.skillType == ESkillType.None || (this.skillType & rSkill.skillType) != 0) &&
                (this.relation == ETargetRelation.None || (this.relation & BattleUtil.CheckRelation(this.executer, rSkill.executer)) != 0))
            {
                var rVirInput = ClassPool.Get<SelectParam>();
                if (this.inputExecuter)
                {
                    rVirInput.unit = this.skill.executer;
                    rVirInput.pos = rVirInput.unit.pos;
                }
                else
                {
                    rVirInput.unit = input.unit;
                    rVirInput.pos = input.pos;
                    rVirInput.aspect = input.aspect;
                }
                this.OnTrigger(rVirInput, skillPart);
            }
        }

        public override void OnReset()
        {
            base.OnReset();
            this.skillType = ESkillType.None;
            this.relation = ETargetRelation.None;
            this.trigger = false;
            this.inputExecuter = false;
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }

#if UNITY_EDITOR
        public override string ToString()
        {
            return "释放技能时";
        }
#endif
    }
}